package com.service.business.product.controller;

import com.service.business.framework.log.RequestLog;
import com.service.business.product.controller.rep.ProductHrComboDetailRep;
import com.service.business.product.controller.rep.ProductHrComboPageRep;
import com.service.business.product.service.MemberCouponService;
import com.service.business.product.service.ProductHrComboService;
import com.service.business.support.SessionUtil;
import com.service.core.rest.BaseController;
import com.service.core.rest.request.PageRequest;
import com.service.core.rest.response.OneResponse;
import com.service.core.rest.response.PageResponse;
import com.service.dao.model.MemberCoupon;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年11月14日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("product-hr-combo")
public class ProductHrComboController extends BaseController {

    @Autowired
    private ProductHrComboService comboService;
    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private MemberCouponService memberCouponService;

    @ApiOperation("套餐分页接口")
    @RequestLog("套餐分页接口")
    @PostMapping("page/{coinsId}/{comboType}")
    public PageResponse<ProductHrComboPageRep> pageCombo(@PathVariable("coinsId") Integer coinsId,
                                                         @PathVariable("comboType") Integer comboType,
                                                         @RequestBody PageRequest request) {
        return successQueryPageResponse(comboService.pageCombo(coinsId,comboType,request));
    }

    @RequestLog("套餐详情")
    @GetMapping("detail/{comboId}")
    public OneResponse<ProductHrComboDetailRep> comboDetail(@PathVariable("comboId")Integer comboId){
        ProductHrComboDetailRep rep = comboService.detail(comboId);
        //查询当前用户的优惠券
        Integer memberId = sessionUtil.getUser().getUserId();
        if(!ObjectUtils.isEmpty(memberId)){
            List<MemberCoupon> couponList = memberCouponService.listByMemberAndProductId(memberId,comboId);
            if(ObjectUtils.isEmpty(couponList)){
                rep.setCouponList(new ArrayList<>());
            }else{
                rep.setCouponList(couponList);
            }
        }else{
            rep.setCouponList(new ArrayList<>());
        }
        return successQueryOneResponse(rep);
    }

    @ApiOperation("推广套餐分页接口")
    @RequestLog("推广套餐分页接口")
    @PostMapping("page-popular")
    public PageResponse<ProductHrComboPageRep> pagePopularCombo(@RequestBody PageRequest pageRequest) {
        return successQueryPageResponse(comboService.pagePopularCombo(pageRequest));
    }

}
